var prefix = "/sys/baseHint"
$().ready(function() {
	    ProvinceBind();
	    validateRule();
});
$("#Province").change(function(){
    CityBind();
})
$("#City").change(function(){
    VillageBind();
})
$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function Bind(str){
    $("#ProviceBind").val(str);
}
function ProvinceBind(){
    $("#Province").html("");
    var str="<option> 请选择 </option>";
    $.ajax({
        url:"/getProvince",
        success:function(data){
            $.each(data.obj,function(i,item){
                str+="<option value="+item.areaId+">"+item.areaName+"</option>"
            })
            $("#Province").append(str)
        },
        error:function(){alert("Error");}
    })
}
function CityBind(){
    $("#City").html("");
    var options=$("#Province option:selected");
    var province=options.val();
    console.log(province);
    if(province==""){
        return;
    }
    $("City").html("");
    var str="<option> 请选择 </option>";

    $.ajax({
        url:"/getChild",
        data:{"parentId":province},
        success:function(data){
            $.each(data.obj,function(i,item){
                str+="<option value="+item.areaId+">"+item.areaName+"</option>";
            })
            $("#City").append(str);
        },
        error:function(){alert("Error");}

    });
}
function VillageBind(){
    $("Village").html("");
    var options=$("#City option:selected");
    var city=options.val();
    if(city==""){
        return;
    }
    $("#Village").html("");
    var str="<option> 请选择 </option>";
    $.ajax({
        url:"/getChild",
        data:{"parentId":city},
        success:function(data){
            $.each(data.obj,function(i,item){
                str+="<option value="+item.areaId+">"+item.areaName+"</option>";
            })
            $("#Village").append(str);
        },
        error:function(){alert("Error");}
    });
}
function save() {
	var areaCode = $('#Province').val() + "," + $('#City').val() + "," + $('#Village').val();
	var province = $("#Province option:selected").text();
	var city = $("#City option:selected").text();
	var village = $("#Village option:selected").text();
	$('#areaCode').val(areaCode);
	$('#areaName').val(province + "," + city + "," + village);
	$.ajax({
		cache : true,
		type : "POST",
		url : prefix + "/insertBaseHint",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.res == 1) {
				parent.layer.msg(data.resMsg);
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);
			} else {
				layer.alert(data.resMsg)
			}
		}
	});
}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			flag : {
				required : true,
			}
		},
		messages : {
			flag : {
				required : icon + "请选择状态",
			}
		}
	})
}